# = test.content.2.rb - Home, Edit Content Test
# 
# See LICENSE.txt for licensing details.
#
# == Description
# This will test the functionality for editing the Inline content on the home page.
#
    require 'watir'
    include Watir
    require 'test/unit'
    require '../watir.config.rb'
    
    
    class TC_EditInlineContentParts_test < Test::Unit::TestCase
      def startup			
        $MainTitle = "Test Title"
        @@ie = Watir::IE.start($SITE_URL)
      end
    
      def ie
        if defined? @@ie
          @@ie
        else
          startup
        end
      end
      
      def test_01_verifyHomeText
        assert(ie.contains_text(/Community Server/))	
      end
      
      def test_02_verifyLoginLinksExist
        if !(ie.link(:text, 'Sign in').exists?)
          ie.goto($SITE_URL + 'logout.aspx')
          ie.refresh
          ie.goto($SITE_URL)
          ie.refresh
        end
      end
      
      def test_03_navigateToLogin
        ie.link(:text, 'Sign in').click
        assert(ie.contains_text("Sign in name"))
      end
      
      def test_04_populateLoginForm
        ie.text_field(:id, /username$/).value = $ADMIN_USER
        assert(ie.text_field(:id, /username$/).verify_contains($ADMIN_USER))
        
        ie.text_field(:id, /password$/).value = $ADMIN_PASSWORD
        assert(ie.text_field(:id, /password$/).verify_contains($ADMIN_PASSWORD))
      end
      
      def test_05_submitLoginForm
        ie.link(:id, /loginButton$/).click
        test_01_verifyHomeText
      end
      
      def test_06_editInlineContentPart
        
          assert(ie.div(:id, /ctl00_ctl01$/).exists?)
          ie.div(:id, /ctl00_ctl01$/).fire_event("ondblclick")
          
          assert(ie.link(:text, 'Save').exists?)
          assert(ie.link(:text, 'Cancel').exists?)
          
          assert(ie.text_field(:id, /ctl00_Editor$/).exists?)
          ie.text_field(:id, /ctl00_Editor$/).set($MainTitle)
          
          # ie.ie.Document.parentWindow.execScript("tinyMCE.getInstanceById(tinyMCE.getEditorId('mce_editor_0')).execCommand('mceSetContent', false, '" + @@MainTitle + "')")
          # ie.ie.Document.parentWindow.execScript("tinyMCE.getInstanceById(tinyMCE.getEditorId('mce_editor_0')).execCommand('mceSetContent', false, '" + @@MainTitle + "')")
          
          #ie.ie.Document.parentWindow.execScript("SiteSettings.UpdateSiteTitle('ctl00_ctl00_bhcr_t', '" + @@MainTitle + "',null)")
          ie.link(:text, 'Save').click
          
          sleep 5 
          ie.refresh
          test_01_verifyHomeText
          
          assert(ie.contains_text($MainTitle))
          sleep 3
          ie.div(:id, /ctl00_ctl01$/).fire_event("ondblclick")
          
          assert(ie.link(:text, 'Save').exists?)
          assert(ie.link(:text, 'Cancel').exists?)
          sleep 3
          #ie.ie.Document.parentWindow.execScript("tinyMCE.getInstanceById(tinyMCE.getEditorId('mce_editor_0')).execCommand('mceSetContent', false, 'Community Server')")
          ie.link(:text, 'Save').click
          sleep 5
          
          ie.refresh          
          test_01_verifyHomeText
          
          assert(ie.contains_text('Community Server'))
      end
      
      def test_08_logoutTryEdit
        ie.goto($SITE_URL + "logout.aspx")
        ie.goto($SITE_URL)
        ie.refresh
        sleep 5 
        assert_raises(UnknownObjectException) { ie.div(:id, /ctl00_ctl01$/).fire_event("onblur") }
      end
      
      def test_99_closeIE
        ie.close
      end
      
    end